/**
 * 定义页面对应的后台处理类完整路径。
 */
var clsUrl = basePath + 'JarpListener?className=cn.com.zsc.income.IncomeMessages';

Ext.onReady(function(){
	Ext.QuickTips.init();

    /**
	 * 带团分类数据源
	 */
	var incomeTypeStore=new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : comboBoxUrl + '43'
		}),
		reader : new Ext.data.JsonReader({
			fields : [
			{name:'CL_CODE'},
			{name:'CL_NAME'}
		]
		}),
		autoLoad : true
	});
	
	/**
	 * 带团分类控件
	 */
	var incomeType = new Ext.form.ComboBox({
		fieldLabel : '带团分类',
		id : 'incomeType',
		width : 90,
		displayField : 'CL_NAME',
		valueField : 'CL_CODE',
		emptyText : '请选择',
		store:incomeTypeStore,
		mode : 'local',
		editable  : false,
		triggerAction : 'all',
		selectOnFocus : true
	});
	
	/**
	 * 观光点数据源
	 */
	var attractionsStore = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
					url : comboBoxUrl + '31'
				}),
		reader : new Ext.data.JsonReader({
					fields : [
							  {name : 'CL_CODE'},
							  {name : 'CL_NAME'}
							 ]
				}),
		autoLoad : true
	});

	/**
	 * 观光点控件
	 */
	var attractions = new Ext.ux.form.LovCombo({
		fieldLabel : '观光点',
		id : 'attractions',
		width : 205,
		displayField : 'CL_NAME',
		valueField : 'CL_CODE',
		store : attractionsStore,
		mode : 'local',
		editable : false,
		triggerAction : 'all'
	});
	
	/**
	 * 带团时间控件
	 */
	var startDate = new Ext.form.DateField({
		id:'startDate',
        fieldLabel:"带团时间",
        name:'startDate',
        format:'Y-m-d',
        editable:false,
        width:90,
        listeners:{
        	'render':function() {
        		var now = new Date();
        		
				var monthFirstDayDate = new Date(now.getYear(), now.getMonth(), 1);
				
        		/*
        		 * 设置开始时间
        		 */
				var year = monthFirstDayDate.getYear();
				var month = monthFirstDayDate.getMonth() + 1;
				var day = monthFirstDayDate.getDate();

				var startDate = year;
				startDate += ((month < 10) ? "-0" : "-") + month+"";
				startDate += ((day < 10) ? "-0" : "-") + day+"";
				
				Ext.getCmp("startDate").setValue(startDate);
				
				var monthLastDayDate = new Date(now.getYear(), now.getMonth() + 1, 0);
		
				/*
        		 * 设置结束时间
        		 */
				year = monthLastDayDate.getYear();
				month = monthLastDayDate.getMonth() + 1;
				day = monthLastDayDate.getDate();
				
				var endDate = year;
				endDate += ((month < 10) ? "-0" : "-") + month+"";
				endDate += ((day < 10) ? "-0" : "-") + day+"";
				
				Ext.getCmp("endDate").setValue(endDate);
        	},
        	'focus':function() {
        		Ext.getCmp("endDate").clearInvalid();
        		Ext.getCmp("startDate").clearInvalid(); 
        	}
        }
    });
    
    /**
	 * 带团时间控件
	 */
	var endDate = new Ext.form.DateField({
		id:'endDate',
        fieldLabel:"至",
        name:'endDate',
        format:'Y-m-d',
        editable:false,
        width:90,
        listeners:{
        	'focus':function() {
        		Ext.getCmp("endDate").clearInvalid();
        		Ext.getCmp("startDate").clearInvalid(); 
        	}
        }
    });

	var storereader= new Ext.data.JsonReader({
    	fields:[
    		{name :'ID'},
    		{name :'GUIDEID'},
    		{name :'GUIDENAME'},
    		{name :'INCOMETYPE'},
    		{name :'ATTRACTIONS'},
    		{name :'RECEPTION'},
    		{name :'SOURCE'},
    		{name :'TERM'},
    		{name :'NOTICEUNIT'},
    		{name :'NOTIFIER'},
    		{name :'NUM'},
    		{name :'STATWIN'},
    		{name :'STARTTIME'},
    		{name :'ENDTIME'},
    		{name :'INVOICE'},
    		{name :'EAMT'},
    		{name :'DISCOUNT'},
    		{name :'RATIO'},
    		{name :'SAMT'},
    		{name :'AAMT'},
    		{name :'SETTLETYPE'},
    		{name :'DEDUCT'},
    		{name :'FEEDBACK'},
    		{name :'REMARKED'},
    		
    		{name :'SHOW_STARLEVEL'},
    		{name :'SHOW_INCOMETYPE'},
    		{name :'SHOW_ATTRACTIONS'},
    		{name :'SHOW_DISCOUNT'}
    	]
	});
		
	/**
	 * 查询员工数据
	 */
	var store = new Ext.data.Store({
		reader:storereader
	});
	
    var searchPanel = new Ext.Panel({
		id:"searchPanel",
		region:"north",
		border:false,
		layout : 'column',
		labelAlign : 'right',
		height:80,
		frame:true,
		labelWidth : 80,
		items:[{
			layout : 'form',
			border:false,
		  	labelWidth : 40,
		  	labelAlign : 'right',
		  	columnWidth : .22,
		  	items:[
		  		   {fieldLabel : '工号',width : 100,name : 'workID',id : 'workID',xtype : 'textfield'},
		  		   {fieldLabel : '姓名',width : 100,name : 'eName',id : 'eName',xtype : 'textfield'}
		  		  ]
		},{
			layout : 'form',
		  	labelWidth : 80,
		  	labelAlign : 'right',
		  	columnWidth : .38,
		  	items:[
		  			{
		  				layout : 'column',
						labelAlign : 'right',
						items:[{
							layout : 'form',
							labelWidth : 80,
							labelAlign : 'right',
							columnWidth : .47,
							items: [startDate]
						},{
							layout : 'form',
							labelWidth : 10,
							labelAlign : 'right',
							columnWidth : .53,
							items: [endDate]
						}]
		  			},
		  			attractions
		  		  ]
		},{
			layout : 'form',
		  	labelWidth : 60,
		  	labelAlign : 'right',
		  	columnWidth : .20,
		  	items:[incomeType]
		},{
			layout : 'form',
		  	labelWidth : 0,
		  	labelAlign : 'right',
		  	columnWidth : .15,
		  	items:[{
	  			layout : 'column',
				labelAlign : 'right',
				items:[{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .5,
				  	items:[
				  		{xtype:'button',width:70,text:'查询',handler:search},
				  		{height:4},
				  		{xtype:'button',width:70,text:'导出Excel',handler:exportExcel}
				  	]
				},{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .5,
				  	items:[
				  		{xtype:'button',width:70,text:'重置',handler:function() {
				  				Ext.getCmp("workID").setValue("");
				  				Ext.getCmp("eName").setValue("");
					  			Ext.getCmp("attractions").setValue("");
					  			Ext.getCmp("incomeType").setValue("");
					  			
					  			var now = new Date();
        		
				        		/*
				        		 * 设置开始时间
				        		 */
								var year = now.getYear();
								var month = now.getMonth() + 1;
								var day = now.getDate();
				
								var startDate = year;
								startDate += ((month < 10) ? "-0" : "-") + month+"";
								startDate += ((day < 10) ? "-0" : "-") + day+"";
								
								Ext.getCmp("startDate").setValue(startDate);
								
								/*
				        		 * 设置结束时间
				        		 */
								now.setDate(now.getDate() + 30);
								year = now.getYear();
								month = now.getMonth() + 1;
								day = now.getDate() + 1;
								
								var endDate = year;
								endDate += ((month < 10) ? "-0" : "-") + month+"";
								endDate += ((day < 10) ? "-0" : "-") + day+"";
								
								Ext.getCmp("endDate").setValue(endDate);
							}
						}
				    ]
				}]
			}]
		}],
		keys: {
            key: 13,
            fn: search
        }
	});
	
	/**
	 * 用户列表
	 */
	var basegrid = new Ext.grid.GridPanel( {
		id :'basegrid',
		store :store,
		split :true,
		bodyStyle:'5px 15px 0',
		region :'center',
		border :false,
		enableColumnMove:false,                                           
	  	enableHdMenu:false,
		frame :false,
		autoScroll :true,
    	columns: [
            new Ext.grid.RowNumberer(),
          	{header:'导游编号',width:80,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'GUIDEID'},
          	{header:'姓名',width:70,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'GUIDENAME'},
          	{header:'员工星级',width:60,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'SHOW_STARLEVEL'},
          	{header:'带团分类',width:60,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'SHOW_INCOMETYPE'},
          	{header:'上团时间',width:150,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'STARTTIME'},
          	{header:'销时时间',width:150,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'ENDTIME'},
          	{header:'应收金额',enableColumnMove:false,sortable:true,hideCollapseTool:false,enableHdMenu:false,enableColumnResize:false,dataIndex:'SAMT'},
			{header:'实收金额',enableColumnMove:false,sortable:true,hideCollapseTool:false,enableHdMenu:false,enableColumnResize:false,dataIndex:'AAMT'},
			{header:'导游提成',width:70,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'DEDUCT'},
			{header:'人数',width:70,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'NUM'},
			{header:'发票号码',width:70,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'INVOICE'},
			{header:'付款方式',width:70,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'SETTLETYPE'},
			{header:'接待单位',width:70,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'RECEPTION'},
			{header:'通知人',width:70,sortable:true,enableColumnMove:false,enableHdMenu:false,enableColumnResize :false, dataIndex: 'NOTIFIER'},
          	{header:'观光点',width:200,enableColumnMove:false,sortable:true,hideCollapseTool:false,enableHdMenu:false,enableColumnResize:false,dataIndex:'SHOW_ATTRACTIONS'}
    	]
	});

	var mainPanel = new Ext.Panel({
            id: 'mainPanel',
            region:'center',
            split:true,
            border:false,
            frame:true,
            layout: 'border',  
            items:[searchPanel,basegrid]
    });
    
	var viewport = new Ext.Viewport( {
		layout:'border',
		items: [mainPanel ]
	});
	
	function search(){
		if(!searchValidate()) {
			return;
		}
		
		mainPanel.body.mask('正在加载数据...','x-mask-loading');
		var obj = new Object();
		obj.workID = Ext.getCmp("workID").getValue();
		obj.eName = Ext.getCmp("eName").getValue();
		obj.startDate = Ext.getCmp("startDate").getRawValue();
		obj.endDate = Ext.getCmp("endDate").getRawValue();
		obj.attractions = Ext.getCmp("attractions").getValue();
		obj.incomeType = Ext.getCmp("incomeType").getValue();
		
    	var url=clsUrl+'&methodName=getIncome&paramCount=0'
		Ext.http.send(url,function(res){
			basegrid.store.loadData(Ext.decode(res));
			mainPanel.body.unmask();
		},Ext.encode(obj));
	}
	
	function exportExcel() {
		var vExportContent = basegrid.getExcelXml(); // 获取数据
		if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) { // 判断浏览器
			var fd = Ext.get('frmDummy');
			if (!fd) {
				fd = Ext.DomHelper.append(Ext.getBody(), {
							tag : 'form',
							method : 'post',
							id : 'frmDummy',
							action : exportUrl,
							target : '_blank',
							name : 'frmDummy',
							cls : 'x-hidden',
							cn : [{
										tag : 'input',
										name : 'exportContent',
										id : 'exportContent',
										type : 'hidden'
									}]
						}, true);
			}
			fd.child('#exportContent').set({
						value : vExportContent
					});
			fd.dom.submit();
		} else {
			document.location = 'data:application/vnd.ms-excel;base64,'+ Base64.encode(vExportContent);
		}
	}
	
	/**
	 * 用户验证
	 */
	function searchValidate(){
		var startDate = Ext.getCmp("startDate").getRawValue();
		var endDate = Ext.getCmp("endDate").getRawValue();
		
		startDate = startDate.replace("-","/").replace("-","/");
		endDate = endDate.replace("-","/").replace("-","/");
		
		var sd = new Date(Date.parse(startDate));
		var ed = new Date(Date.parse(endDate));
		
		if (parseInt(Math.abs(ed-sd)/1000/60/60/24)>31){
			Ext.getCmp("startDate").markInvalid("起止时间不能大于1个月");
			Ext.getCmp("endDate").markInvalid("起止时间不能大于1个月");
			return false; 
		}
		
		if(Ext.getCmp("startDate").getValue() > Ext.getCmp("endDate").getValue()) {
			Ext.getCmp("startDate").markInvalid("订立合同开始时间必须小于结束时间");
			return false;
		}

		return true;
	}

	search();
});